package tools

import (
	"src/db"
	"src/db/db_modle"
	"src/modle"
	"strconv"
	"strings"
	"sync"
)

func CheckUid(uid uint) bool {
	if uid == 0 {
		return false
	}
	return true
}

func Contains(m map[uint]func(*chan modle.DetailsType, *sync.WaitGroup, uint, db_modle.RiskRuleBase), i interface{}) bool {
	for k := range m {
		if k == i {
			return true
		}
	}
	return false
}

// 分数和通过关系
func PassCheck(score int) (modle.Enum, string) {
	min := db.GetScoreStandard(1)
	max := db.GetScoreStandard(2)
	if score < min.Score {
		return modle.Failed, min.Reason
	} else if score <= max.Score {
		return modle.Review, ""
	} else {
		return modle.Pass, ""
	}
}

// 原因拼接
func ReasonStr(exp string, min uint, max uint) string {
	minnum := strconv.FormatUint(uint64(min), 10)
	//log.Debug("拼接最小值:", minnum)
	minReplaced := strings.Replace(exp, LessThan, minnum, 1)

	maxnum := strconv.FormatUint(uint64(max), 10)
	//log.Debug("拼接最大值:", maxnum)
	ReasonStr := strings.Replace(minReplaced, BigThan, maxnum, 1)

	return ReasonStr
}

// 数据填充
func FillDetails(oid, value uint, status, score int, reason string, ch *chan modle.DetailsType, sy *sync.WaitGroup) {
	a := modle.DetailsType{
		Oid:    oid,
		Score:  score,
		Status: status,
		Reason: reason,
		Value:  value,
	}
	*ch <- a
	sy.Done()
	return
}

// 超出限额判断
func OutLimit(data uint, ch *chan modle.DetailsType, sy *sync.WaitGroup, rrb db_modle.RiskRuleBase, deduction int) {
	if data <= rrb.Min || data >= rrb.Max {
		// 条件不符
		reason := ReasonStr(rrb.Explanation, rrb.Min, rrb.Max)
		FillDetails(rrb.Oid, data, ExecuteSuccessNotPass, deduction, reason, ch, sy)
		return
	} else {
		// 符合条件
		FillDetails(rrb.Oid, data, ExecuteSuccessAndPass, 0, "", ch, sy)
		return
	}
}

// 包含类型的范围限制
func ContainLimit(data uint, ch *chan modle.DetailsType, sy *sync.WaitGroup, rrb db_modle.RiskRuleBase, deduction int) {
	// 如果包含则不符合
	if data >= rrb.Min && data <= rrb.Max {
		// 条件不符
		reason := ReasonStr(rrb.Explanation, rrb.Min, rrb.Max)
		FillDetails(rrb.Oid, data, ExecuteSuccessNotPass, deduction, reason, ch, sy)
		return
	} else {
		// 符合条件
		FillDetails(rrb.Oid, data, ExecuteSuccessAndPass, 0, "", ch, sy)
		return
	}
}

// 大于范围限制
func BigThanLimit(data uint, ch *chan modle.DetailsType, sy *sync.WaitGroup, rrb db_modle.RiskRuleBase, deduction int) {
	if data >= rrb.Max {
		// 条件不符
		reason := ReasonStr(rrb.Explanation, rrb.Min, rrb.Max)
		FillDetails(rrb.Oid, data, ExecuteSuccessNotPass, deduction, reason, ch, sy)
		return
	} else {
		// 符合条件
		FillDetails(rrb.Oid, data, ExecuteSuccessAndPass, 0, "", ch, sy)
		return
	}
}

// 小于等于范围限制
func SmallAndEqualLimit(data uint, ch *chan modle.DetailsType, sy *sync.WaitGroup, rrb db_modle.RiskRuleBase, deduction int) {
	if data <= rrb.Min {
		// 条件不符
		reason := ReasonStr(rrb.Explanation, rrb.Min, rrb.Max)
		FillDetails(rrb.Oid, data, ExecuteSuccessNotPass, deduction, reason, ch, sy)
		return
	} else {
		// 符合条件
		FillDetails(rrb.Oid, data, ExecuteSuccessAndPass, 0, "", ch, sy)
		return
	}
}

// 小于范围限制
func SmallThanLimit(data uint, ch *chan modle.DetailsType, sy *sync.WaitGroup, rrb db_modle.RiskRuleBase, deduction int) {
	if data < rrb.Min {
		// 条件不符
		reason := ReasonStr(rrb.Explanation, rrb.Min, rrb.Max)
		FillDetails(rrb.Oid, data, ExecuteSuccessNotPass, deduction, reason, ch, sy)
		return
	} else {
		// 符合条件
		FillDetails(rrb.Oid, data, ExecuteSuccessAndPass, 0, "", ch, sy)
		return
	}
}

// logID ,用于识别区分用户的不同指标的不同log
func GetLogId(uid uint, oid uint) string {
	return " 用户id:" + strconv.FormatUint(uint64(uid), 10) + " 指标id:" + strconv.FormatUint(uint64(oid), 10)
}
